/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.product;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.product.ProductInventoryDetailDao;
import com.neusoft.lohas.modules.ivm.entity.product.ProductInventoryDetail;

/**
 * 商品入库记录Service
 * @author Jackie.Liu
 * @version 2014-12-30
 */
@Component
@Transactional(readOnly = true)
public class ProductInventoryDetailService extends BaseService {

	@Autowired
	private ProductInventoryDetailDao productInventoryDetailDao;
	
	public ProductInventoryDetail get(String id) {
		return productInventoryDetailDao.get(id);
	}
	
	public Page<ProductInventoryDetail> find(Page<ProductInventoryDetail> page, ProductInventoryDetail productInventoryDetail) {
		DetachedCriteria dc = productInventoryDetailDao.createDetachedCriteria();
		dc.add(Restrictions.eq(ProductInventoryDetail.FIELD_DEL_FLAG, ProductInventoryDetail.DEL_FLAG_NORMAL));
		if (productInventoryDetail.getPurchase()!=null && StringUtils.isNotBlank(productInventoryDetail.getPurchase().getId())) {
			dc.add(Restrictions.eq("purchase.id", productInventoryDetail.getPurchase().getId()));
		}
		dc.add(Restrictions.eq("product.id", productInventoryDetail.getProduct().getId()));
		dc.addOrder(Order.desc("createDate"));
		return productInventoryDetailDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ProductInventoryDetail productInventoryDetail) {
		productInventoryDetailDao.save(productInventoryDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		productInventoryDetailDao.deleteById(id);
	}

}
